import { query } from '../services/testResult';
import { isSuccess } from '../configs/error';
import createModel from './base';

export default createModel({
  namespace: 'testResult',

  state: {
    list: [],
    permissionIds: []
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        // dispatch({
        //   type: 'query'
        // });
        if (location.pathname === '/testResult') { // 每次刷新一下，获取最新
        	// dispatch({
        	// 	type: 'query'
        	// });
        }
      });
    },
  },

  effects: {
    *query({ payload = {} }, { call, put, select }) {
      if (payload && (payload.current)) {
        yield put({
          type: 'set',
          payload: {
            page: payload.current
          }
        });
      }
      const data = yield call(query, payload);
      if (isSuccess(data)) {
        const resultData = data.data;
				const products = resultData.records;
        yield put({
          type: 'set',
          payload: {
            list: products,
            total: resultData.total,
            pageSize: resultData.size,
            current: resultData.current,
          },
        });
      }
    },
  },
  reducers: {},
}, query);
